# -*- coding: utf-8 -*-
from django.template import RequestContext
from django.template import Context
from django.db.models import Q
from django.core.context_processors import csrf
from django.http import HttpResponseRedirect,Http404
from django.shortcuts import render_to_response
from django.contrib.auth.decorators import login_required
from django.core.paginator import Paginator,PageNotAnInteger, EmptyPage

from sprt import createExtInfo,get_date_range
from sprt.admin.models import Dept,get_my_depts,get_my_branchs2
from sprt.checkin.models import CheckinLog,update_last_logout,update_first_login


@login_required
def index(request):
    pn = int(request.GET.get('pageNum',1))
    dt1,dt2 = get_date_range(request.POST.get('dt1'),request.POST.get('dt2'))
    c = RequestContext(request)
    dt1Str = dt1.strftime('%Y-%m-%d')
    dt2Str = dt2.strftime('%Y-%m-%d')
    paginator = Paginator(CheckinLog.objects.filter(user=request.user, created_date__gte=dt1Str, created_date__lte=dt2Str),20)
    try:
        data_list = paginator.page(pn)
    except PageNotAnInteger:
        data_list = paginator.page(1)
    except EmptyPage:
        data_list = paginator.page(paginator.num_pages)

    c["list"] = data_list
    c["nav_url"]="%s?dt1=%s&amp;dt2=%s&amp;pageNum=%s" % (request.path,dt1Str,dt2Str,"%d")
    c["pc"] = paginator.num_pages
    c["pn"] = pn
    c["dt1"] = dt1
    c["dt2"] = dt2
    createExtInfo(request,c)
    return render_to_response("checkin/list.html",c)


@login_required
def current_month_absence(request):
    pn = int(request.GET.get('pageNum',1))
    dt1,dt2 = get_date_range(request.POST.get('dt1'),request.POST.get('dt2'))
    c = RequestContext(request)
    dt1Str = dt1.strftime('%Y-%m-%d')
    dt2Str = dt2.strftime('%Y-%m-%d')
    paginator = Paginator(CheckinLog.objects.filter(Q(user=request.user),Q(created_date__gte=dt1Str), Q(created_date__lte=dt2Str), Q(first_login_time=None)|Q(last_logout_time=None)),20)
    try:
        data_list = paginator.page(pn)
    except PageNotAnInteger:
        data_list = paginator.page(1)
    except EmptyPage:
        data_list = paginator.page(paginator.num_pages)

    c["list"] = data_list
    c["nav_url"]="%s?dt1=%s&amp;dt2=%s&amp;pageNum=%s" % (request.path,dt1Str,dt2Str,"%d")
    c["pc"] = paginator.num_pages
    c["pn"] = pn
    c["dt1"] = dt1
    c["dt2"] = dt2
    createExtInfo(request,c)
    return render_to_response("checkin/list.html",c)

@login_required
def current_month_dept_absence(request):
    pn = int(request.GET.get('pageNum',1))
    dt1,dt2 = get_date_range(request.POST.get('dt1'),request.POST.get('dt2'))
    c = RequestContext(request)
    dt1Str = dt1.strftime("%Y-%m-%d")
    dt2Str = dt2.strftime("%Y-%m-%d")
    query_set = CheckinLog.objects.raw("select C.* from CHECKIN_LOG C inner join auth_user A on C.user_id = A.id inner join USER_DETAIL U2 on A.id = U2.user_id inner join DEPT T on U2.dept_id = T.id where C.created_date >='%s' and C.created_date <='%s' and T.principal_auth_user_id = %d and (C.first_login_time is null or C.last_logout_time is null)" %(dt1Str,dt2Str,request.user.id))
    paginator = Paginator(query_set,20)
    paginator._count = len(list(query_set)) 
    try:
        data_list = paginator.page(pn)
    except PageNotAnInteger:
        data_list = paginator.page(1)
    except EmptyPage:
        data_list = paginator.page(paginator.num_pages)
    c["who"] = u"我负责的部门缺勤:"
    c["list"] = data_list
    c["nav_url"]="%s?dt1=%s&amp;dt2=%s&amp;pageNum=%s" % (request.path,dt1Str,dt2Str,"%d")
    c["pc"] = paginator.num_pages
    c["pn"] = pn
    c["dt1"] = dt1
    c["dt2"] = dt2
    createExtInfo(request,c)
    return render_to_response("checkin/list.html",c)

@login_required
def checkin(request):
    update_first_login(request.user)
    return HttpResponseRedirect("/checkin/my")


@login_required
def checkout(request):
    update_last_logout(request.user)
    return HttpResponseRedirect("/checkin/my")

@login_required
def view_user(request, id):
    try:
        from django.contrib.auth.models import User
        user = User.objects.get(pk=id)
    except User.DoesNotExist:
        raise Http404
    if user.id != request.user.id and not request.META["management_role"]:
        raise Http404
    pn = int(request.GET.get('pageNum',1))
    dt1,dt2 = get_date_range(request.POST.get('dt1'),request.POST.get('dt2'))
    c = RequestContext(request)
    dt1Str = dt1.strftime("%Y-%m-%d")
    dt2Str = dt2.strftime("%Y-%m-%d")
    paginator = Paginator(CheckinLog.objects.filter(user=user,created_date__gte=dt1Str,created_date__lte=dt2Str),20)
    try:
        data_list = paginator.page(pn)
    except PageNotAnInteger:
        data_list = paginator.page(1)
    except EmptyPage:
        data_list = paginator.page(paginator.num_pages)
    c["list"] =data_list
    c["nav_url"]="%s?dt1=%s&amp;dt2=%s&amp;pageNum=%s" % (request.path,dt1Str,dt2Str,"%d")
    c["pc"] = paginator.num_pages
    c["pn"] = pn
    c["dt1"] = dt1
    c["dt2"] = dt2
    createExtInfo(request,c)
    return render_to_response("checkin/list.html",c)

@login_required
def my_dept(request, id):
    pn = int(request.GET.get('pageNum',1))
    try:
        dept = Dept.objects.get(pk=id)
    except Dept.DoesNotExist:
        raise Http404
    
    if dept == None or dept.principal_auth_user_id != request.user.id:
        raise Http404
    dt1,dt2 = get_date_range(request.POST.get('dt1'),request.POST.get('dt2'))
    c = RequestContext(request)
    c["my_dept"] = dept
    c["dt1"] = dt1
    c["dt2"] = dt2
    dt1Str = dt1.strftime("%Y-%m-%d")
    dt2Str = dt2.strftime("%Y-%m-%d")
    query_set = CheckinLog.objects.raw("select C.* from CHECKIN_LOG C inner join auth_user A on C.user_id = A.id inner join USER_DETAIL U2 on A.id = U2.user_id where C.created_date >= '%s' and C.created_date <= '%s' and U2.dept_id = %s " %(dt1Str,dt2Str,dept.id))
    paginator = Paginator(query_set,20)
    paginator._count = len(list(query_set))    
    
    try:
        data_list = paginator.page(pn)
    except PageNotAnInteger:
        data_list = paginator.page(1)
    except EmptyPage:
        data_list = paginator.page(paginator.num_pages)
    
    c["list"] = data_list
    c["nav_url"]="%s?dt1=%s&amp;dt2=%s&amp;pageNum=%s" % (request.path,dt1Str,dt2Str,"%d")
    c["pc"] = paginator.num_pages
    c["pn"] = pn
    createExtInfo(request,c)
    return render_to_response("checkin/list.html",c)

@login_required
def branch(request,id):
    pn = int(request.GET.get('pageNum',1))
    dt1,dt2 = get_date_range(request.POST.get('dt1'),request.POST.get('dt2'))    
    try:
        dept = Dept.objects.get(pk=id)
    except Dept.DoesNotExist:
        raise Http404
    branchs = get_my_branchs2(request.user)
    if dept == None or dept not in branchs:
        raise Http404
    c = RequestContext(request)
    c["dept"]=dept
    dt1Str = dt1.strftime("%Y-%m-%d")
    dt2Str = dt2.strftime("%Y-%m-%d")
    query_set = CheckinLog.objects.raw("select C.* from CHECKIN_LOG C inner join auth_user A on C.user_id = A.id inner join USER_DETAIL U2 on A.id = U2.user_id where C.created_date >= '%s' and C.created_date <='%s' and U2.dept_id = %s " %(dt1Str,dt2Str,id))
    paginator = Paginator(query_set,20)
    paginator._count = len(list(query_set))    
    
    try:
        data_list = paginator.page(pn)
    except PageNotAnInteger:
        data_list = paginator.page(1)
    except EmptyPage:
        data_list = paginator.page(paginator.num_pages)
    
    c["list"] = data_list
    c["nav_url"]="%s?dt1=%s&amp;dt2=%s&amp;pageNum=%s" % (request.path,dt1Str,dt2Str,"%d")
    c["pc"] = paginator.num_pages
    c["pn"] = pn
    c["dt1"] = dt1
    c["dt2"] = dt2
    createExtInfo(request,c)
    return render_to_response("checkin/list.html",c)
